#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QPushButton>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
    QMessageBox* msg = new QMessageBox(this);
    msg->setWindowTitle("message");
    msg->setText("message");
    msg->setIcon(QMessageBox::Question);
    msg->setStandardButtons(QMessageBox::Open|QMessageBox::Close);
    msg->setAttribute(Qt::WA_DeleteOnClose);

    //添加自定义按钮
    QPushButton* button = new QPushButton("按钮");
    msg->addButton(button,QMessageBox::AcceptRole);

    //根据返回的值，判断点击的哪个标准按钮
    int re = msg->exec();
    if(re==QMessageBox::Open)
    {
        qDebug()<<"open";
    }
    else if(re==QMessageBox::Close)
    {
        qDebug()<<"close";
    }
}

void MainWindow::on_pushButton_2_clicked()
{
    //用静态函数构建
    int re = QMessageBox::question(this,"标题","对话框文本",QMessageBox::Open|QMessageBox::Close);

    if(re==QMessageBox::Open)
    {
        qDebug()<<"open";
    }
    else if(re==QMessageBox::Close)
    {
        qDebug()<<"close";
    }
}
